package com.ucap.weking.guangan.fragments.publicFragment;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ucap.weking.guangan.Activitys.NewContentActivity;
import com.ucap.weking.guangan.R;
import com.ucap.weking.guangan.adapter.LIstAdapter;
import com.ucap.weking.guangan.base.BaseFragment;
import com.ucap.weking.guangan.bean.ListItemBean;
import com.ucap.weking.guangan.httpTools.HttpCallRequest;
import com.ucap.weking.guangan.modle.ItemModel;
import com.ucap.weking.guangan.views.LoadingListView;
import com.ucap.weking.guangan.views.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by weking on 2017/10/17.
 * 列表数据通用页面
 */

public class HomeContentPublic extends BaseFragment {
    @InjectView(R.id.lv_content)
    LoadingListView lvContent;
    @InjectView(R.id.srlRefresh)
    SwipeRefreshLayout srlRefresh;
    @InjectView(R.id.tv_is_null)
    TextView tvIsNull;
    private LIstAdapter adapter;
    private int numow = 1;//页面开始角标
    private int num = 10;//页面结束角标
    public String contentUri;//页面数据地址
    public String contentName;//页面名称
    private boolean isImage = true;

    public HomeContentPublic() {

    }

    public HomeContentPublic setInfo(String contentUri, String contentName) {
        this.contentUri = contentUri;
        this.contentName = contentName;
        return this;
    }

    public HomeContentPublic setInfo(String contentUri, String contentName, boolean isImage) {
        this.contentUri = contentUri;
        this.contentName = contentName;
        this.isImage = isImage;
        return this;
    }

    @Override
    public void setData() {
        numow = 1;
        num = 10;
        final ArrayList<ItemModel> mData = new ArrayList<>();
        adapter = new LIstAdapter(mData);
        lvContent.setAdapter(adapter);
        getData(mData, numow, num);
        srlRefresh.setColorSchemeResources(R.color.colorPrimary);
        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                numow = 1;
                num = 10;
                mData.clear();
                getData(mData, numow, num);
            }
        });

        lvContent.setInterface(new LoadingListView.IloadListener() {
            @Override
            public void onLoad() {
                getData(mData, numow += 10, num += 10);
            }
        });
        lvContent.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mData.size() > 0 && position > -1 && position < mData.size())
                    startContentActivity(adapter.getItem(position).getContentUri());
            }
        });
    }

    private void getData(final ArrayList<ItemModel> mData, final int startNum, int endNum) {
        if (startNum == 1)
            srlRefresh.setRefreshing(true);
        HttpCallRequest.callRequest(contentUri + "numtwo=" + startNum + "&num=" + endNum, new HttpCallRequest.OnCallListener() {
            @Override
            public void onRequest(final String body) {
                Log.i("number", "numow:  " + numow + " num: " + num);
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        ListItemBean listItemBean = gson.fromJson(body, ListItemBean.class);
                        if (listItemBean != null) {
                            ArrayList<ItemModel> data = new ArrayList<>();
                            List<ListItemBean.RowsBean> rows2 = listItemBean.getRows();
                            if (rows2 != null && rows2.size() > 0) {
                                for (ListItemBean.RowsBean bean2 : rows2) {
                                    ItemModel model = new ItemModel(bean2.getTITLE(), bean2.getTIME(), bean2.getID());
                                    if (contentName.equals("常务会议"))//常务会议不显示列表图片
                                        model.setImage(false);
                                    else
                                        model.setImage(true);
                                    model.setImageId(bean2.getPATHANDURL());
                                    model.setContentUri(bean2.getURL());
                                    data.add(model);
                                }
                                mData.addAll(data);
                                adapter.notifyDataSetChanged();
                                srlRefresh.setRefreshing(false);
                                lvContent.loadComplete();
                                tvIsNull.setVisibility(View.GONE);
                            } else {
                                MyToast.showTrue(getContext(), "已加载全部！");
                                srlRefresh.setRefreshing(false);
                                lvContent.loadComplete();
                                tvIsNull.setVisibility(View.GONE);
                                if (numow > 1) {
                                    numow -= 10;
                                    num -= 10;
                                }
                            }
                        } else {
                            MyToast.showTrue(getContext(), "已加载全部！");
                            srlRefresh.setRefreshing(false);
                            lvContent.loadComplete();
                            tvIsNull.setVisibility(View.GONE);
                            if (numow > 1) {
                                numow -= 10;
                                num -= 10;
                            }
                        }


                    }
                });
            }

            @Override
            public void onErr(Exception e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        srlRefresh.setRefreshing(false);
                        lvContent.loadComplete();
                        adapter.notifyDataSetChanged();
                        if (numow > 1) {
                            numow -= 10;
                            num -= 10;
                        }
                        if (mData.size() < 1) {
                            tvIsNull.setVisibility(View.VISIBLE);
                        } else {
                            tvIsNull.setVisibility(View.GONE);
                            MyToast.showTrue(getContext(), "连接超时，请检查网路连接！");
                        }
                        Log.i("number", "err   numow:  " + numow + " num: " + num);
                    }
                });

            }
        });
    }

    public void startContentActivity(String uri) {
        Intent intent = new Intent(getActivity(), NewContentActivity.class);
        intent.putExtra("title", contentName);
        intent.putExtra("uri", uri);
        getActivity().startActivity(intent);

    }

    @Override
    public void intiView() {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.frgament_content1_news, null);
        ButterKnife.inject(this, view);
        flContent.addView(view);
    }
}
